package org.bluedream.core.module.sys.config.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bluedream.comm.base.BaseController;
import org.bluedream.core.config.advice.RsaSecurityParameter;
import org.bluedream.core.module.sys.config.entity.Config;
import org.bluedream.core.module.sys.config.service.ConfigService;
import org.bluedream.core.module.sys.entity.User;
import org.bluedream.core.utils.UserUtil;
import org.bluedream.core.utils.configUtil.GoalConfigUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @ClassName ConfigController
 * @Description TODO
 * @Author foxsand
 * @Data 2021-06-23 8:41
 * @Version
 */
@Slf4j
@Controller
@RequestMapping(value = "${adminPath}/config")
public class ConfigController extends BaseController {
    @Resource
    private ConfigService configService;

    @ModelAttribute
    public Config get(String id){
        return configService.getById(id);
    }

    @RequiresPermissions("config:view")
    @RequestMapping(value = {"/list" , ""})
    public String list(Config config, Model model){
        model.addAttribute("isSystem" , UserUtil.isSystem());
        model.addAttribute("pageSize" , GoalConfigUtil.getValueByKey("sys.page.size"));
        model.addAttribute("config" , config);
        return "sys/config/configList";
    }

    @RequiresPermissions("config:view")
    @RequestMapping("/form")
    public String form(Config config , Model model){
        model.addAttribute("config" , config);
        return "sys/config/configForm";
    }

    @RequiresPermissions("config:view")
    @RequestMapping("/findPage")
    @ResponseBody
    public IPage<Config> findPage(@RsaSecurityParameter Config config){
        return configService.findPage(config);
    }

    @RequiresPermissions("config:edit")
    @RequestMapping("/save")
    @ResponseBody
    public String save(Config config){
        boolean flag = configService.save(config);
        return "用户信息保存成功";
    }
}
